/**
 * TorrentJEDI
 * 		Torrent Java Episode Download Interface
 */
package torrentjedi.data;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import torrentjedi.core.Configuration;
import torrentjedi.core.TorrentSource;

/**
 * @author Alper Kokmen
 *
 */
public class SourceConfigurationWriter {
	private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	private static final String TORRENT_SOURCES_START = "<" + ConfigurationHandler.TAG_TORRENT_SOURCES + ">";
	private static final String TORRENT_SOURCES_END = "</" + ConfigurationHandler.TAG_TORRENT_SOURCES + ">";
	private static final String SOURCE_START = "<" + ConfigurationHandler.TAG_SOURCE + ">";
	private static final String SOURCE_END = "</" + ConfigurationHandler.TAG_SOURCE + ">";
	private static final String NAME_START = "<" + ConfigurationHandler.TAG_NAME + ">";
	private static final String NAME_END = "</" + ConfigurationHandler.TAG_NAME + ">";
	private static final String URL_START = "<" + ConfigurationHandler.TAG_URL + ">";
	private static final String URL_END = "</" + ConfigurationHandler.TAG_URL + ">";
	private static final String ICON_START = "<" + ConfigurationHandler.TAG_ICON + ">";
	private static final String ICON_END = "</" + ConfigurationHandler.TAG_ICON + ">";
	private static final String FAVORITE_START = "<" + ConfigurationHandler.TAG_FAVORITE + ">";
	private static final String FAVORITE_END = "</" + ConfigurationHandler.TAG_FAVORITE + ">";
	private static final String UPDATE_INTERVAL_START = "<" + ConfigurationHandler.TAG_UPDATE_INTERVAL + ">";
	private static final String UPDATE_INTERVAL_END = "</" + ConfigurationHandler.TAG_UPDATE_INTERVAL + ">";
	private static final String AUTO_LOAD_START = "<" + ConfigurationHandler.TAG_AUTO_LOAD + ">";
	private static final String AUTO_LOAD_END = "</" + ConfigurationHandler.TAG_AUTO_LOAD + ">";
	
	private Configuration configuration;
	
	public SourceConfigurationWriter(Configuration configuration) {
		super();
		this.configuration = configuration;
	}
	
	public Boolean writeSourceConfiguration(TorrentSource newSource) {
		String configFile = getConfiguration().getSourceConfigString();
		
		try {
			FileWriter fileWriter = new FileWriter(configFile);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			
			/*
			 * write the source configuration file
			 */
			bufferedWriter.write(XML_HEADER);
			bufferedWriter.write("\n");
			bufferedWriter.write(TORRENT_SOURCES_START);
			bufferedWriter.write("\n\t");
			
			for (TorrentSource source : getConfiguration().getSourceConfiguration().getSources()) {
				bufferedWriter.write(SOURCE_START);
				bufferedWriter.write("\n\t\t");
				
				// <name> ... </name>
				bufferedWriter.write(NAME_START);
				bufferedWriter.write(source.getName());
				bufferedWriter.write(NAME_END);
				bufferedWriter.write("\n\t\t");
				
				// <url> ... </url>
				bufferedWriter.write(URL_START);
				bufferedWriter.write(source.getUrl());
				bufferedWriter.write(URL_END);
				bufferedWriter.write("\n\t\t");
				
				// <icon> ... </icon>
				bufferedWriter.write(ICON_START);
				bufferedWriter.write(source.getIcon());
				bufferedWriter.write(ICON_END);
				bufferedWriter.write("\n\t\t");
				
				// <favorite> ... </favorite>
				bufferedWriter.write(FAVORITE_START);
				if (source.getFavorite() == true) {
					bufferedWriter.write("yes");
				} else {
					bufferedWriter.write("no");
				}
				bufferedWriter.write(FAVORITE_END);
				bufferedWriter.write("\n\t\t");
				
				// <update-interval> ... </update-interval>
				bufferedWriter.write(UPDATE_INTERVAL_START);
				bufferedWriter.write(source.getUpdateInterval().toString());
				bufferedWriter.write(UPDATE_INTERVAL_END);
				bufferedWriter.write("\n\t\t");
				
				// <auto-load> ... </auto-load>
				bufferedWriter.write(AUTO_LOAD_START);
				if (source.getAutoLoad() == true) {
					bufferedWriter.write("yes");
				} else {
					bufferedWriter.write("no");
				}
				bufferedWriter.write(AUTO_LOAD_END);
				bufferedWriter.write("\n\t");
				
				bufferedWriter.write(SOURCE_END);
				bufferedWriter.write("\n\t");
			}
			
			// <-------------- new source
			bufferedWriter.write(SOURCE_START);
			bufferedWriter.write("\n\t\t");
			
			// <name> ... </name>
			bufferedWriter.write(NAME_START);
			bufferedWriter.write(newSource.getName());
			bufferedWriter.write(NAME_END);
			bufferedWriter.write("\n\t\t");
			
			// <url> ... </url>
			bufferedWriter.write(URL_START);
			bufferedWriter.write(newSource.getUrl());
			bufferedWriter.write(URL_END);
			bufferedWriter.write("\n\t\t");
			
			// <icon> ... </icon>
			bufferedWriter.write(ICON_START);
			bufferedWriter.write(newSource.getIcon());
			bufferedWriter.write(ICON_END);
			bufferedWriter.write("\n\t\t");
			
			// <favorite> ... </favorite>
			bufferedWriter.write(FAVORITE_START);
			if (newSource.getFavorite() == true) {
				bufferedWriter.write("yes");
			} else {
				bufferedWriter.write("no");
			}
			bufferedWriter.write(FAVORITE_END);
			bufferedWriter.write("\n\t\t");
			
			// <update-interval> ... </update-interval>
			bufferedWriter.write(UPDATE_INTERVAL_START);
			bufferedWriter.write(newSource.getUpdateInterval().toString());
			bufferedWriter.write(UPDATE_INTERVAL_END);
			bufferedWriter.write("\n\t\t");
			
			// <auto-load> ... </auto-load>
			bufferedWriter.write(AUTO_LOAD_START);
			if (newSource.getAutoLoad() == true) {
				bufferedWriter.write("yes");
			} else {
				bufferedWriter.write("no");
			}
			bufferedWriter.write(AUTO_LOAD_END);
			bufferedWriter.write("\n\t");
			
			bufferedWriter.write(SOURCE_END);
			bufferedWriter.write("\n");
			// new source --------------------->
			
			bufferedWriter.write(TORRENT_SOURCES_END);
			
			bufferedWriter.close();
			
		} catch (IOException e) {
			System.out.println("ERROR@TorrentJEDI:SourceConfigurationWriter updateSourceConfiguration failed to write the config file");
		}
		
		return true;
	}

	/**
	 * @return the configuration
	 */
	public Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * @param configuration the configuration to set
	 */
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

}
